package com.lsp.kotlingank.mainpage.activity

import android.support.v7.widget.LinearLayoutManager
import android.view.View
import com.lsp.base.BaseActivity
import com.lsp.data.RemoteDataManager
import com.lsp.data.bean.GankInfoList
import com.lsp.data.net.ApiError
import com.lsp.data.net.HttpError
import com.lsp.data.net.UiSubscriber
import com.lsp.kotlingank.R
import com.lsp.lib_uikit.commonrecycler.adapter.CommonRvAdapter
import kotlinx.android.synthetic.main.activity_main.*
import org.jetbrains.anko.toast
import rx.android.schedulers.AndroidSchedulers
import rx.schedulers.Schedulers

class MainActivity : BaseActivity() {

    var commonAdapter: CommonRvAdapter<GankInfoList.GankInfo>? = null
    override fun onClick(v: View?) {

    }

    override fun initVariable() {
    }

    override fun initTitleBar(): Boolean {
        setTitleLeft(R.mipmap.arrow_left_grey) {
            finish()
        }

        setTitleCenter("gank.io", null)

        return true
    }

    override fun initView() {
        initRecyclerView()
    }

    private fun initRecyclerView() {
        loadmoreRecyclerView.layoutManager = LinearLayoutManager(this)
        commonAdapter = CommonRvAdapter(this, R.layout.item_gank_info)
        loadmoreRecyclerView.adapter = commonAdapter
    }

    override fun initData() {
        manageRetrofitCall(RemoteDataManager.getInstance().planbookList
                .observeOn(AndroidSchedulers.mainThread()).
                subscribeOn(Schedulers.io()), object : UiSubscriber<GankInfoList>() {
            override fun onSuccess(result: GankInfoList?) {
                toast("success")
                if (result == null) {
                } else {
                    commonAdapter?.addAllAndNotifyChanged(result.gankInfo, true)
                }
            }

            override fun onApiError(apiError: ApiError) {
                toast("apiError")
            }

            override fun onHttpError(httpError: HttpError) {
                toast("httpError")
            }

            override fun onVerifyError() {
                toast("onVerifyError")
            }

            override fun onEnd() {
            }

        })

    }

    override fun getLayoutId(): Int {
        return R.layout.activity_main
    }

}
